<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>文章列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css" />
    <link rel="stylesheet" href="./css/reset.css" />
    <link rel="stylesheet" href="./css/iconfont.css" />
    <link rel="stylesheet" href="./css/main.css" />
    <link rel="stylesheet" href="./libs/layui/css/layui.css" />
    <script src="./libs/jquery/jquery.min.js"></script>
    <script src="./js/jqconfig.js"></script>
    <script src="./libs/layui/layui.js"></script>
  </head>

  <body>
    <div class="container-fluid">
      <div class="common_title">文章列表</div>
      <div class="container-fluid common_con">
        <div class="row opt_btns">
          <div class="col-xs-6">
            <div class="form-inline">
              <input
                type="text"
                id="aname"
                class="form-control input-sm"
                placeholder="输入文章标题..."
              />
              <select id="selCategory" name="" class="form-control input-sm">
                <option value="">所有分类</option>
              </select>
              <select id="selStatus" name="" class="form-control input-sm">
                <option value="">所有状态</option>
                <option value="草稿">草稿</option>
                <option value="已发布">已发布</option>
              </select>
              <button id="btnSearch" class="btn btn-default btn-sm">
                筛选
              </button>
            </div>
          </div>
          <div class="col-xs-6">
            <a
              href="article_add.html"
              class="btn btn-success btn-sm pull-right"
              id="release_btn"
              >发表文章</a
            >
          </div>
        </div>
        <table class="table table-striped table-bordered table-hover mp20">
          <thead>
            <tr>
              <th>标题</th>
              <th>作者</th>
              <th>分类</th>
              <th class="text-center">发表时间</th>
              <th class="text-center">状态</th>
              <th class="text-center" width="100">操作</th>
            </tr>
          </thead>
          <tbody id="tbody">
            <tr>
              <td>王积千造统最头</td>
              <td>杰克</td>
              <td>奇趣事</td>
              <td class="text-center">2017-06-08 07:08:46</td>
              <td class="text-center">已发布</td>
              <td class="text-center">
                <a href="article_edit.html" class="btn btn-default btn-xs"
                  >编辑</a
                >
                <a
                  href="javascript:void(0);"
                  class="btn btn-danger btn-xs delete"
                  >删除</a
                >
              </td>
            </tr>
          </tbody>
        </table>
        <div class="row text-center" id="paging"></div>
      </div>
    </div>
    <script src="./libs/art-template/template-web.js"></script>
    <script type="text/html" id="t1">
      {{each data.data}}
      <tr>
        <td>{{$value.title}}</td>
        <td>{{$value.author}}</td>
        <td>{{$value.category}}</td>
        <td class="text-center">{{$value.date}}</td>
        <td class="text-center">{{$value.state}}</td>
        <td class="text-center">
          <button onclick="edit({{$value.id}})" class="btn btn-default btn-xs">
            编辑
          </button>
          <a
            href="javascript:void(0);"
            class="btn btn-danger btn-xs delete"
            onclick="dellist({{$value.id}})"
            >删除</a
          >
        </td>
      </tr>
      {{/each}}
    </script>
    <script type="text/html" id="t2">
      {{each data}}
      <option value="{{$value.id}}">{{$value.name}}</option>

      {{/each}}
    </script>
    <script>
      // 引入laypage
      var laypage, layer;
      layui.use("laypage", function () {
        laypage = layui.laypage;
        seach();
        //执行一个laypage实例

        getcrealist();

        // 点击筛选
        regClick();

        layui.use("layer", function () {
          layer = layui.layer;
        });
      });

      //   定义ajax的data对象
      let params = { page: 1, perpage: 6 };
      // 搜索
      function seach() {
        $.ajax({
          type: "get",
          url: "/admin/article/query",
          data: params,
          success: (res) => {
            console.log(res);
            let htmlstr = template("t1", res);
            $("#tbody").html(htmlstr);

            // 调用页码模板
            laypage.render({
              elem: "paging", //注意，这里的 test1 是 ID，不用加 # 号
              count: res.data.totalCount, //数据总数，从服务端得到
              //   让单前的页面为起始页
              curr: params.page,

              jump: function (obj, first) {
                if (!first) {
                  params.page = obj.curr;
                  seach();
                }
              },
            });
          },
        });
      }

      function getcrealist() {
        // 通过后台接口拿到id
        $.ajax({
          type: "get",
          url: "/admin/category/list",

          success: (res) => {
            console.log(res);
            $("#selCategory").append(template("t2", res));
          },
        });
      }

      function regClick() {
        //   点击筛选按钮
        $("#btnSearch").click(function () {
          // 拿到文本框对应的值  然后付给对齐的全局对象params
          params.key = $("#aname").val().trim();
          params.type = $("#selCategory").val().trim();
          params.state = $("#selStatus").val().trim();
          // 然单前的页码为1
          params.page = 1;
          // 在调用一次全局
          seach();
        });
      }

      // 点击删除按钮
      function dellist(id) {
        console.log(1);

        layer.confirm(
          "是否确认删除?",
          { icon: 3, title: "删除提示" },
          function (index) {
            //do something

            layer.close(index);

            $.ajax({
              type: "post",
              url: "/admin/article/delete",
              data: { id },
              success: (res) => {
                console.log(res);
                seach();
              },
            });
          }
        );
      }

      //点击编辑按钮
      function edit(id) {
        let editId = id;
        console.log(editId);
        // 点击保存到本地
        localStorage.setItem("editId", editId);
        // 跳转到指定页面
        location.href = "./article_edit.html";
      }
    </script>
  </body>
</html>
